package com.xianzhiOA.service.impl;/**
 * Created by Administrator on 2017/5/17.
 */

import com.xianzhiOA.bean.XzApprovalProcess;
import com.xianzhiOA.bean.XzApprovalProcessInfo;
import com.xianzhiOA.dao.XzApprovalProcessInfoMapper;
import com.xianzhiOA.dao.XzApprovalProcessMapper;
import com.xianzhiOA.service.XzApprovalProcessInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 审批节点实例表
 *
 * @author 王诗博
 * @create 2017-05-17 14:30
 **/
@Service("XzApprovalProcessInfoService")
public class XzApprovalProcessInfoServiceImpl implements XzApprovalProcessInfoService {
     @Autowired
      private XzApprovalProcessInfoMapper approvalProcessInfoMapper;
     @Autowired
     private XzApprovalProcessMapper approvalProcessMapper;
    //添加审批节点实例
    @Override
    public int insert(Long templetBaseId,Long templetBaseInfoId){
        int i=0;
        List<XzApprovalProcess> approvalProcessList=approvalProcessMapper.selByTempletBaseId(templetBaseId);
        System.out.println("approvalProcessList:"+approvalProcessList);
        if(approvalProcessList.size()>0){
            XzApprovalProcessInfo approvalProcessInfo=new XzApprovalProcessInfo();
            Date newtime = new Date();
            for(int j=0;j<approvalProcessList.size();j++){
                approvalProcessInfo.setTempletBaseId(templetBaseId);
                approvalProcessInfo.setNodeId(approvalProcessList.get(j).getNodeId());
                approvalProcessInfo.setRoleId(approvalProcessList.get(j).getRoleId());
                approvalProcessInfo.setTempletBaseInfoId(templetBaseInfoId);
                approvalProcessInfo.setAuditStatus("0");
                approvalProcessInfo.setAuditTime(newtime);
                i=approvalProcessInfoMapper.insert(approvalProcessInfo);
            }
        }
        return i;
    }
    //撤回申请（删除节点实例表的方法）
    @Override
    public int phoneyDeleteByPrimaryKey(XzApprovalProcessInfo approvalProcessInfo) {
        int i=0;
        i=approvalProcessInfoMapper.phoneyDeleteByPrimaryKey(approvalProcessInfo);
        return i;
    }
    //修改节点实例表中的审批状态
    @Override
    public int updateState(Long templetBaseInfoId, String auditStatus) {
        int i=0;
        Map map=new HashMap();
        map.put("templetBaseInfoId",templetBaseInfoId);
        map.put("auditStatus",auditStatus);
        i=approvalProcessInfoMapper.updateState(map);
        return i;
    }
    //根据模板实例id查询所有
    @Override
    public List<XzApprovalProcessInfo> selectByPrimaryKey(Long templetBaseInfoId) {
        List<XzApprovalProcessInfo> approvalProcessInfo=approvalProcessInfoMapper.selectByPrimaryKey(templetBaseInfoId);
        return approvalProcessInfo;
    }

    //根据节点实例ID查询
    @Override
    public XzApprovalProcessInfo selectById(Long approvalprocessInfoId) {
        XzApprovalProcessInfo approvalProcessInfo=approvalProcessInfoMapper.selectById(approvalprocessInfoId);
        return approvalProcessInfo;
    }

    //申请详情的回退功能
    @Override
    public int updateStateToInProcess(Long templetBaseInfoId, int nodeId) {
        int i=0;
        Map<String,Object> map=new HashMap<String,Object>();
        map.put("templetBaseInfoId",templetBaseInfoId);
        map.put("nodeId",nodeId);

        i=approvalProcessInfoMapper.updateStateToInProcess(map);
        return i;
    }
}
